package com.cyt.cytcrm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyt.cytcrm.entity.Employee;
import com.cyt.cytcrm.entity.Role;
import com.cyt.cytcrm.service.IEmployeeService;
import com.cyt.cytcrm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RestController
public class RoleController {


    @Autowired
    private IRoleService service;

    @GetMapping("/roles")
    public List<Role> roles(){

        return service.list();
    }


    @GetMapping("/role")
    public Map getList( Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Role> r = new Page<Role>();
        r.setSize(limit);
        r.setCurrent(page);

        IPage<Role> iPage=service.selectList(r);
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }

    /**
     *  新增用户
     */
    @PostMapping("/role")
    public Map add(Role role,Integer deptId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.add(role,deptId));
        System.out.println("aa"+role);
        System.out.println("aa"+deptId);

        return result;
    }

    /**
     *  删除用户
     */
    @DeleteMapping("/role/del/{roleId}")
    public Map del(@PathVariable Integer roleId){
        Role role = new Role();
        role.setRoleId(roleId);
        role.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.updateById(role));
        return result;
    }

    /**
     *  修改用户
     */
    @PutMapping("/role")
    public Map edit(Role role,Integer deptId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",service.update(role,deptId));
        return result;
    }
}
